//列表查询
export function getQueryList(url, params, that) {
    return new Promise((resolve, reject) => {
        that.$get(url, params).then(res => {
            if (res.code == 200) {
                let arr = res.data.data;
                let arrList = [];
                arr.forEach((item, index) => {
                    arrList.push(Object.assign({}, item, {
                        'xuHao': index + 1
                    }))
                })
                that.locationList = arrList;
                that.total = res.data.total;
                that.loading = false;
            } else {
                that.$message.error(res.msg);
            }

        })

    });
}

export function addData(url, params, that) {
    return new Promise((resolve, reject) => {
        that.$post(url, params).then(res => {
            if (res.code == 200) {
                that.editVisible = false;
                that.params.page = 1;
                that.getLocationList();
                that.$message.success('创建成功！');
            } else {
                that.editVisible = true;
                that.$message.error(res.msg);
            }

        })

    });
}

export function editData(url, params, that) {
    return new Promise((resolve, reject) => {
        that.$put(url, params).then(res => {
            if (res.code == 200) {
                that.editVisible = false;
                that.getLocationList();
                that.$message.success('编辑成功！');
            } else {
                that.editVisible = true;
                that.$message.error(res.msg);
            }

        })

    });
}

export function deleteData(url, tip, that) {
    return new Promise((resolve, reject) => {
        that.$confirm(tip, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            that.$delete(url).then(res => {
                console.log('res',res)
                if (res.code == 200) {
                    that.getLocationList();
                    that.$message.success('删除成功！');
                } else {
                    that.$message.error(res.msg);
                }
            })
        }).catch(() => {
            that.$message({
                type: 'info',
                message: '已取消删除'
            });
        });

    });
}

export default {
    getQueryList,
    addData,
    editData,
    deleteData,
}
